package com.namuan.jp_stock.util;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import com.namuan.jp_stock.object.Account;
import com.namuan.jp_stock.object.ActivityComment;
import com.namuan.jp_stock.object.Timestamp;
import com.namuan.jp_stock.activity.UserProfileActivity;
import com.namuan.jp_stock.R;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


public class ActivityCommentItemAdapter extends ArrayAdapter<ActivityComment> {
	private Justplace mJutplace;
	private AsyncJustplaceRunner mAsyncRunner;

	private int mResource;
	private ArrayList<ActivityComment> mList;
	private LayoutInflater mInflater;
	private Activity activity;
	private ProfileImageView activityUserImg;
	private TextView userProfileIdText;
	private String loginId;
	
	public CustomImageManager imageManager;
	private TextView activityTimeStamp;
	private TextView activityCommentText;

	/**
	 * @param context
	 * @param layoutResource
	 * @param objects
	 */
	public ActivityCommentItemAdapter(Activity a, int layoutResource,
			ArrayList<ActivityComment> objects) {
		super(a, layoutResource, objects);
		this.activity = a;
		this.mResource = layoutResource;
		this.mList = objects;
		this.mInflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageManager = new CustomImageManager(activity.getApplicationContext(),
				R.drawable.defaultuser);

		mJutplace = new Justplace();
		SessionStore.restore(mJutplace, a);
		loginId = mJutplace.getmUserNickname();
		mAsyncRunner = new AsyncJustplaceRunner(mJutplace);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos = position;
		final ActivityComment activityComment = mList.get(position);

		if (convertView == null) {
			convertView = mInflater.inflate(mResource, null);
		}

		if (activityComment != null) {
			activityUserImg = (ProfileImageView) convertView.findViewById(R.id.activityUserImg);
			userProfileIdText = (TextView) convertView.findViewById(R.id.activityUserNickname);
			
			activityTimeStamp = (TextView) convertView.findViewById(R.id.activityTimeStamp);			
			activityCommentText = (TextView) convertView.findViewById(R.id.activityComment);

			//1. 이미지뷰 : 유저이미지
			if (activityComment.getProfilepic() != null
					&& !activityComment.getProfilepic().equals("null")) {
				activityUserImg.setTag(Justplace.PROFILEIMG_URL
						+ activityComment.getProfilepic());
				Log.d("!!!!!!!!!!!!!!!!!!!!!!!!", Justplace.PROFILEIMG_URL
						+ activityComment.getProfilepic());
				imageManager.displayImage(
						Justplace.PROFILEIMG_URL + activityComment.getProfilepic(),
						activity, activityUserImg);

			} else {
				activityUserImg.setTag(null);
				imageManager.displayImage(activityComment.getProfilepic(), activity,
						activityUserImg);
			}
			
			activityUserImg.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent userprofileintent = new Intent(activity,
							UserProfileActivity.class);
					userprofileintent.putExtra("userid", activityComment.getUserid());
					activity.startActivity(userprofileintent);
					activity.overridePendingTransition(R.anim.fade, R.anim.hold);
				}
			});

			// 2. 텍스트 : 유저id
			userProfileIdText.setText(activityComment.getUserid());
			
			userProfileIdText.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent userprofileintent = new Intent(activity,
							UserProfileActivity.class);
					userprofileintent.putExtra("userid", activityComment.getUserid());
					activity.startActivity(userprofileintent);
					activity.overridePendingTransition(R.anim.fade, R.anim.hold);
				}
			});
			
			// 3. 텍스트 : 타임 스탬프
			activityTimeStamp.setText(new Timestamp().timestmsp(activityComment.getTimestamp()));
			// 4. 텍스트 : 코멘트
			activityCommentText.setText(activityComment.getComment());

			

		}
		return convertView;
	}

}
